package com.zf.shuashua.ui.mine;

import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.AddBlack;
import com.zf.shuashua.bean.BlackList;
import com.zf.shuashua.utils.ToastUtil;

import java.util.List;

import io.reactivex.Observable;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlackListFragment extends BaseListFragment<BlackList.DataBean> {

    private RecyclerView rv;
    private BlackListAdapter mAdapter;

    public static BlackListFragment getFragment() {
        return new BlackListFragment();
    }

    @Override
    protected void initData() {
        loadPage(1);
    }

    @Override
    protected RecyclerView getRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(mActivity));
        return rv;
    }

    @Override
    protected MeiBaseAdapter<BlackList.DataBean> getAdapter() {
        mAdapter = new BlackListAdapter(R.layout.fans_item, null);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, final View view, int position) {
                BlackList.DataBean dataBean = (BlackList.DataBean) adapter.getData().get(position);
                if (dataBean.getBlack() != null) {
                    HttpData.getInstance().delBlank(Integer.toString(dataBean.getBlack_user_id()), new ACallback<AddBlack>() {
                        @Override
                        public void onSuccess(AddBlack data) {
                            if (data.getCode() == 200) {
                                mAdapter.remove(position);
                                ToastUtil.show(mActivity, "取消成功");
                            }
                        }

                        @Override
                        public void onFail(int errCode, String errMsg) {

                        }
                    });

                }
            }
        });

        return mAdapter;
    }

    @Override
    protected Observable<Result<List<BlackList.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getBlackList(pageNo);
        return getListData(observable);
    }


    private Observable<Result<List<BlackList.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<BlackList.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    BlackList likeVideo = ParseJsonUtils.parseData(s, BlackList.class);
                    result.data = likeVideo.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }


    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_black_list;
    }
}
